<script setup lang="ts">
import { reactive } from "vue";
import { onLoad, onReachBottom } from "@dcloudio/uni-app";
import { storeToRefs } from "pinia";
import { orderTypes } from "@/constants";
import useHomeStore from "@/pinia/modules/homeStore";
import useUserStore from "@/pinia/modules/userStore";
import type { PageParams } from "@/types/home";
import Good from "@/components/good.vue";

const homeStore = useHomeStore();

const userStore = useUserStore();

const { safeAreaInsets } = uni.getSystemInfoSync();

const { guessList, guessListTotal } = storeToRefs(homeStore);

const { userInfo } = storeToRefs(userStore);

/*获取猜你喜欢列表的分页参数*/
const pageParams = reactive<PageParams>({
    page: 1,
    pageSize: 10,
});

/*获取猜你喜欢列表的函数*/
const fetchGuess = async () => {
    try {
        await homeStore.fetchGuess(pageParams);
    } catch (error: any) {
        await uni.showToast({ title: error.message, icon: "error", duration: 2000 });

    }
};

onLoad(() => {
    fetchGuess();
});

onReachBottom(() => {
    if (guessList.value.length >= guessListTotal.value) return uni.showToast({ title: "商品加载完毕", icon: "error", duration: 2000 });
    fetchGuess();
});
</script>

<template>
    <view class="user ">
        <!-- 个人资料 -->
        <view class="profile" :style="{ paddingTop: safeAreaInsets!.top + 'px' }">
            <!-- 情况1：已登录 -->
            <view class="overview" v-if="userInfo.id">
                <navigator url="/subPackages/profile/profile" hover-class="none">
                    <image class="avatar" mode="aspectFill" :src="userInfo.avatar"></image>
                </navigator>
                <view class="meta">
                    <view class="nickname">{{ userInfo.account }}</view>
                    <navigator class="extra" url="/subPackages/profile/profile" hover-class="none">
                        <text class="update">更新头像昵称</text>
                    </navigator>
                </view>
            </view>
            <!-- 情况2：未登录 -->
            <view class="overview" v-else>
                <navigator url="/pages/login/login" hover-class="none">
                    <image class="avatar gray" mode="aspectFill" src="http://yjy-xiaotuxian-dev.oss-cn-beijing.aliyuncs.com/picture/2021-04-06/db628d42-88a7-46e7-abb8-659448c33081.png"></image>
                </navigator>
                <view class="meta">
                    <navigator url="/pages/login/login" hover-class="none" class="nickname">未登录</navigator>
                    <view class="extra">
                        <text class="tips">点击登录账号</text>
                    </view>
                </view>
            </view>
            <navigator class="settings" url="/subPackages/settings/settings" hover-class="none">设置</navigator>
        </view>
        <!-- 我的订单 -->
        <view class="orders">
            <view class="title">
                <text>我的订单</text>
                <navigator class="navigator" url="/pagesOrder/list/list?type=0" hover-class="none">
                    <text>查看全部订单</text>
                    <text class="icon-right"></text>
                </navigator>
            </view>
            <view class="section">
                <!-- 订单 -->
                <navigator v-for="item in orderTypes" :key="item.type" :class="item.icon" :url="`/pagesOrder/list/list?type=${item.type}`" class="navigator" hover-class="none">{{ item.text }}</navigator>
                <!-- 客服 -->
                <button class="contact icon-handset" open-type="contact">售后</button>
            </view>
        </view>
        <!-- 猜你喜欢 -->
        <view class="guess-list">
            <view class="caption">
                <text class="text">猜你喜欢</text>
            </view>
            <view class="guess">
                <good :good="item" v-for="(item) in guessList" :key="item.id"></good>
            </view>
        </view>
    </view>
</template>

<style lang="scss">
.user {
    height: 100%;
    overflow: hidden;
    background-repeat: no-repeat;
    background-image: url(https://pcapi-xiaotuxian-front-devtest.itheima.net/miniapp/images/center_bg.png);
    background-size: 100% auto;

    .profile {
        margin-top: 20rpx;
        position: relative;

        .overview {
            display: flex;
            height: 120rpx;
            padding: 0 36rpx;
            color: #fff;
        }

        .avatar {
            width: 120rpx;
            height: 120rpx;
            border-radius: 50%;
            background-color: #eee;
        }

        .gray {
            filter: grayscale(100%);
        }

        .meta {
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: flex-start;
            line-height: 30rpx;
            padding: 16rpx 0;
            margin-left: 20rpx;
        }

        .nickname {
            max-width: 350rpx;
            margin-bottom: 16rpx;
            font-size: 30rpx;

            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
        }

        .extra {
            display: flex;
            font-size: 20rpx;
        }

        .tips {
            font-size: 22rpx;
        }

        .update {
            padding: 3rpx 10rpx 1rpx;
            color: rgba(255, 255, 255, 0.8);
            border: 1rpx solid rgba(255, 255, 255, 0.8);
            margin-right: 10rpx;
            border-radius: 30rpx;
        }

        .settings {
            position: absolute;
            bottom: 0;
            right: 40rpx;
            font-size: 30rpx;
            color: #fff;
        }
    }

    .orders {
        position: relative;
        z-index: 99;
        padding: 30rpx;
        margin: 50rpx 20rpx 0;
        background-color: #fff;
        border-radius: 10rpx;
        box-shadow: 0 4rpx 6rpx rgba(240, 240, 240, 0.6);

        .title {
            height: 40rpx;
            line-height: 40rpx;
            font-size: 28rpx;
            color: #1e1e1e;

            .navigator {
                font-size: 24rpx;
                color: #939393;
                float: right;
            }
        }

        .section {
            width: 100%;
            display: flex;
            justify-content: space-between;
            padding: 40rpx 20rpx 10rpx;

            .navigator,
            .contact {
                text-align: center;
                font-size: 24rpx;
                color: #333;

                &::before {
                    display: block;
                    font-size: 60rpx;
                    color: #ff9545;
                }
            }

            .contact {
                padding: 0;
                margin: 0;
                border: 0;
                background-color: transparent;
                line-height: inherit;
            }
        }
    }

    .guess-list {
        background-color: #f7f7f8;

        .caption {
            display: flex;
            justify-content: center;
            line-height: 1;
            padding: 36rpx 0 40rpx;
            font-size: 32rpx;
            color: #262626;

            .text {
                display: flex;
                justify-content: center;
                align-items: center;
                padding: 0 28rpx 0 30rpx;

                &::before,
                &::after {
                    content: '';
                    width: 20rpx;
                    height: 20rpx;
                    background-image: url(@/static/images/bubble.png);
                    background-size: contain;
                    margin: 0 10rpx;
                }
            }
        }

        .guess {
            display: flex;
            flex-wrap: wrap;
            justify-content: space-evenly;
        }
    }
}


</style>
